home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1999 May: Tool Chest / Developer CD Series Tool Chest (Apple Computer)(May 1999).iso / Tool Chest / Development Kits / MPW etc / MPW-GM / MPW / Examples / CFMExamples / ModApp / Windows.c < prev   
Encoding:
C/C++ Source or Header  |  1998-12-03  |  7.1 KB  |  317 lines  |  [TEXT/MPS ]

  1. /*
  2.     File:        Windows.c
  3.  
  4.     Contains:    Code to handle our application's windows
  5.  
  6.     Written by:    Richard Clark
  7.  
  8.     Copyright:    © 1993,1994 by Apple Computer, Inc., all rights reserved.
  9.  
  10.     Change History (most recent first):
  11.  
  12.                  8/22/94    BLS        Fixed up includes.
  13.                  1/27/94    RC        Set the clip region before dragging a tool window
  14.                                      (in case we have something which will redraw the window
  15.                                     completely after a move.)
  16.                  1/26/94    RC        Made DrawClippedGrowIcon publically accessable,
  17.                                      added code to set the clipping region to omit the
  18.                                     grow icon.
  19.                   11/28/93    RC        First release
  20.  
  21.     To Do:
  22. */
  23.  
  24. #ifndef __TYPES__
  25.     #include <Types.h>
  26. #endif
  27.  
  28. #ifndef __QUICKDRAW__
  29.     #include <Quickdraw.h>
  30. #endif
  31.  
  32. #ifndef __QDOFFSCREEN__
  33.     #include <QDOffscreen.h>
  34. #endif
  35.  
  36. #include <Menus.h>
  37. #include <Devices.h>
  38. #include <Events.h> 
  39.  
  40. #ifndef __MEMORY__
  41.     #include <Memory.h>
  42. #endif
  43.  
  44. #ifndef __TEXTUTILS__
  45.     #include <TextUtils.h>
  46. #endif
  47.  
  48. #ifndef __DIALOGS__
  49.     #include <Dialogs.h>
  50. #endif
  51.  
  52.  
  53. #include "ModApp.h"
  54. #include "Prototypes.h"
  55. #include "ToolAPI.h"
  56.  
  57. void AlertUser (short messageCode, short errorNum)
  58. // Tell the user that something went wrong...
  59. {
  60.     Str255        messageString;
  61.     Str255        numberString;
  62.     
  63.     if (messageCode > 0)
  64.         GetIndString(messageString, rErrorStrings, messageCode);
  65.     else
  66.      messageString[0] = '\0';
  67.      
  68.     if (errorNum != 0)
  69.         NumToString(errorNum, numberString);
  70.     else
  71.         numberString[0] = '\0';
  72.      
  73.     ParamText (messageString, numberString, (StringPtr)"\p", (StringPtr)"\p");
  74.     
  75.     StopAlert(kErrorDialog, NULL);
  76. }
  77.  
  78.  
  79. void DrawClippedGrowIcon (WindowPtr wp)
  80. {
  81.     /* Draw the grow icon without drawing the scroll bar lines */
  82.     Rect        portRect = wp->portRect;
  83.     RgnHandle    oldClip = NULL;
  84.     Rect        newClip;
  85.  
  86.     SetPort(wp);
  87.     oldClip = NewRgn();
  88.     GetClip(oldClip);
  89.     
  90.     /* Draw *only* the grow icon */
  91.     newClip = portRect;
  92.     newClip.top = newClip.bottom - 15;
  93.     newClip.left = newClip.right - 15;
  94.     ClipRect(&newClip);
  95.     DrawGrowIcon(wp);
  96.  
  97.     SetClip(oldClip);
  98.     DisposeRgn(oldClip);
  99. }
  100.  
  101.  
  102. void NewDisplayWindow ()
  103. {
  104.     DrawingWindowPeek    newWindow;
  105.     Ptr                    storage;
  106.  
  107.     storage = NewPtrClear(sizeof(DrawingWindow));
  108.     newWindow = (DrawingWindowPeek)GetNewCWindow(kDisplayWindow, storage, (WindowPtr)-1L);
  109.     if (newWindow) {
  110.         SetPort((WindowPtr)newWindow);
  111.         ShowWindow((WindowPtr)newWindow); /* Has to be visible for "GetGlobalBounds" to work */
  112.     }
  113.     gCurrentWindow = FrontWindow();
  114. } /* NewDisplayWindow */
  115.  
  116.  
  117. void CloseAWindow (WindowPtr wp)
  118. {
  119.     int16        wKind;
  120.     OSErr        err;
  121.     
  122.     if (wp) {
  123.         wKind = ((WindowPeek)wp)->windowKind;
  124.         if (wKind < 0)
  125.             CloseDeskAcc(wKind);
  126.         else if (wKind == userKind) {
  127.             // Release the tool
  128.             err = UnloadTool(wp);
  129.             CloseWindow(wp);
  130.             DisposeHandle(((DrawingWindowPeek)wp)->currMenuBar);
  131.             DisposePtr((Ptr)wp);
  132.             UseMenuBar(NULL);
  133.             AdjustMenus();
  134.         }
  135.         gCurrentWindow = FrontWindow();
  136.     }
  137. } /* CloseAWindow */
  138.  
  139.  
  140. /*----------------------- Handle window update events ----------------------*/
  141.  
  142. void DoUpdate (WindowPtr wp)
  143. {    
  144.     SetPort(wp);                         /* make update window active grafPort    */
  145.     BeginUpdate(wp);                    /* visRgn temporarily = updateRgn        */
  146.     EraseRect(&wp->portRect);
  147.     
  148.     if (isUserWindow(wp)) {
  149.         DrawingWindowPeek    aWindow = (DrawingWindowPeek)wp;
  150.  
  151.         SetPort(wp);
  152.         if ((aWindow->connectionID == 0) && (aWindow->toolResource == NULL)) {
  153.             // We don't have a tool loaded
  154.             Str255    noToolMessage = "\pPlease select an item from the Modules menu";
  155.             
  156.             TextFace(0);
  157.             TextSize(0);
  158.             TextFont(1);
  159.             MoveTo(2, 20);
  160.             TETextBox(&noToolMessage[1],noToolMessage[0],&wp->portRect,teJustCenter);
  161.         } else if (aWindow->toolRoutines.toolUpdateProc) {
  162.             CallToolUpdateProc(aWindow->toolRoutines.toolUpdateProc, wp);
  163.         }
  164.         DrawClippedGrowIcon(wp);
  165.     }
  166.     EndUpdate(wp);                        /* restore normal visRgn of grafport    */
  167. } /* DoUpdate */
  168.  
  169.  
  170. void DoActivate (EventRecord *theEvent)
  171. {
  172.     WindowPtr    wp = (WindowPtr)theEvent->message;
  173.     Boolean        activeFlag = theEvent->modifiers & 1;
  174.     
  175.     SetPort(wp);
  176.     if (isUserWindow(wp)) {
  177.         DrawingWindowPeek    aWindow = (DrawingWindowPeek)wp;
  178.  
  179.         DrawClippedGrowIcon(wp);
  180.         if (activeFlag)
  181.             UseMenuBar(aWindow->currMenuBar);
  182.         else
  183.             UseMenuBar(NULL);
  184.         if (aWindow->toolRoutines.toolWindowActivateProc)
  185.             CallToolWindowActivateProc(aWindow->toolRoutines.toolWindowActivateProc, wp, activeFlag);
  186.     }
  187.     AdjustMenus();
  188. } /* DoActivate */
  189.  
  190.  
  191. void DoContentClick (WindowPtr wp, EventRecord *theEvent)
  192. {
  193.  
  194.     if (wp != FrontWindow())
  195.         SelectWindow(wp);
  196.     else {
  197.         SetPort(wp);
  198.         if (isUserWindow(wp)) {
  199.             DrawingWindowPeek    aWindow = (DrawingWindowPeek)wp;
  200.             if (aWindow->toolRoutines.toolClickProc)
  201.                 CallToolWindowClickProc(aWindow->toolRoutines.toolClickProc, wp, theEvent);
  202.         }
  203.     }
  204. } /* DoContentClick */
  205.  
  206.  
  207. void DoGrowWindow(WindowPtr wp, EventRecord *theEvent)
  208. {
  209.     Rect                sizeRect;
  210.     long                newSize;
  211.  
  212.     sizeRect = qd.screenBits.bounds;
  213.     sizeRect.top = 100;
  214.     sizeRect.left = 100;
  215.     newSize = GrowWindow(wp, theEvent->where, &sizeRect);
  216.     if (newSize != 0) {
  217.         SizeWindow(wp, newSize & 0x0000FFFF, newSize >> 16, true);
  218.         SetPort(wp);
  219.         InvalRect(&wp->portRect);
  220.         if (isUserWindow(wp)) {
  221.             DrawingWindowPeek    aWindow = (DrawingWindowPeek)wp;
  222.             
  223.             if (aWindow->toolRoutines.toolWindowResizedProc)
  224.                 CallToolWindowResizedProc(aWindow->toolRoutines.toolWindowResizedProc, wp);
  225.         }
  226.     }
  227. }
  228.  
  229.  
  230. void DoDragWindow(WindowPtr wp, EventRecord *theEvent)
  231. {
  232.     Rect                dragRect;
  233.  
  234.     dragRect = qd.screenBits.bounds;
  235.     dragRect.top = 40;
  236.     DragWindow(wp, theEvent->where, &dragRect);
  237.     if (isUserWindow(wp)) {
  238.         DrawingWindowPeek    aWindow = (DrawingWindowPeek)wp;
  239.  
  240.         /* Update the gWorld in case the screen depth changed */
  241.         if (aWindow->toolRoutines.toolWindowMovedProc) {
  242.             SetPort(wp);
  243.             ClipToContentArea(wp);
  244.             CallToolWindowMovedProc(aWindow->toolRoutines.toolWindowMovedProc, wp);
  245.             ResetClip(wp);
  246.         }
  247.     }
  248. }
  249.  
  250.  
  251. void DoZoomWindow(WindowPtr wp, EventRecord *theEvent, int16 windowPart)
  252. {
  253.     Point                globalPt = theEvent->where;
  254.  
  255.     SetPort(wp);
  256.     if (TrackBox(wp, globalPt, windowPart)) {
  257.         ZoomWindow(wp, windowPart, true);
  258.         InvalRect(&wp->portRect);
  259.         if (isUserWindow(wp)) {
  260.             DrawingWindowPeek    aWindow = (DrawingWindowPeek)wp;
  261.  
  262.             if (aWindow->toolRoutines.toolWindowResizedProc)
  263.                 CallToolWindowResizedProc(aWindow->toolRoutines.toolWindowResizedProc, wp);
  264.         }
  265.     }
  266. }
  267.  
  268.  
  269. void DoIdleWindow (WindowPtr wp)
  270. {
  271.     
  272.     if (isUserWindow(wp)) {
  273.         DrawingWindowPeek    aWindow = (DrawingWindowPeek)wp;
  274.  
  275.         if (aWindow->toolRoutines.toolIdleProc) {
  276.             SetPort(wp);
  277.             ClipToContentArea(wp);
  278.             CallToolIdleProc(aWindow->toolRoutines.toolIdleProc, wp);
  279.             ResetClip(wp);
  280.         }
  281.     }
  282. } /* DoIdleWindow */
  283.  
  284.  
  285. void ClipToContentArea (WindowPtr wp)
  286. // Set the window's clipping region to exclude the grow icon
  287. {
  288.     RgnHandle    newClip = NewRgn();
  289.     Rect        growIconRect;
  290.     RgnHandle    growIconRgn = NewRgn();
  291.     
  292.     if ((newClip != NULL) && (growIconRgn != NULL)) {
  293.         SetPort(wp);
  294.             growIconRect = wp->portRect;
  295.             growIconRect.top = growIconRect.bottom - 15;
  296.             growIconRect.left = growIconRect.right - 15;
  297.             RectRgn(growIconRgn, &growIconRect);
  298.             RectRgn(newClip, &wp->portRect);
  299.             DiffRgn(newClip, growIconRgn, newClip);
  300.             SetClip(newClip);
  301.     }
  302.     if (newClip)
  303.         DisposeRgn(newClip);
  304.     if (growIconRgn)
  305.         DisposeRgn(growIconRgn);
  306. }
  307.  
  308.  
  309. void ResetClip (WindowPtr wp)
  310. {
  311.     Rect    maxClip = {-32767, -32767, 32767, 32767};
  312.     
  313.     SetPort(wp);
  314.     ClipRect(&maxClip);
  315. }
  316.  
  317.